//
//  UIView+Frame.m
//  一些常用类的封装
//
//  Created by IOS on 2016/12/26.
//  Copyright © 2016年 IOS. All rights reserved.
//

#import "UIView+Category.h"
@implementation UIView (Category)

- (void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x{
    return self.frame.origin.x;
}

- (void)setMaxX:(CGFloat)maxX{
    self.x = maxX - self.width;
}

- (CGFloat)maxX{
    return CGRectGetMaxX(self.frame);
}

- (void)setMaxY:(CGFloat)maxY{
    self.y = maxY - self.height;
}

- (CGFloat)maxY{
    return CGRectGetMaxY(self.frame);
}

- (void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size{
    return self.frame.size;
}

//设置手势点击事件 block回调
//- (void)setTapActionWithBlock:(void (^)(void))block {
//    UITapGestureRecognizer *tap = objc_getAssociatedObject(self, &LHCTActionHandlerTapGestureKey);
//    if (!tap){
//        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)];
//        [self addGestureRecognizer:tap];
//        objc_setAssociatedObject(self, &LHCTActionHandlerTapGestureKey, tap, OBJC_ASSOCIATION_RETAIN);
//    }
//    objc_setAssociatedObject(self, &LHCTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
//}
//
////设置手势消息转发处理
//- (void)__handleActionForTapGesture:(UITapGestureRecognizer *)tap{
//    if (tap.state == UIGestureRecognizerStateRecognized) {
//        void(^action)(void) = objc_getAssociatedObject(self, &LHCTActionHandlerTapBlockKey);
//        if (action){action();}
//    }
//}

-(void)addShadow{
//    [self addShadowColor:AppUI_Color_Shadow_Info offset:CGSizeMake(0, 2)];
}

- (void)addShadowColor:(UIColor *)color
{
    [self addShadowColor:color offset:CGSizeMake(0, 2)];
}

-(void)addShadowColor:(UIColor *)color offset:(CGSize)offsert
{
    UIView * shadowView= [[UIView alloc] init];
    shadowView.backgroundColor = [UIColor whiteColor];
    // 禁止将 AutoresizingMask 转换为 Constraints
    shadowView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.superview insertSubview:shadowView belowSubview:self];
    // 添加 right 约束
    NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:shadowView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    [self.superview addConstraint:rightConstraint];
    
    // 添加 left 约束
    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:shadowView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    [self.superview addConstraint:leftConstraint];
    // 添加 top 约束
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:shadowView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    [self.superview addConstraint:topConstraint];
    // 添加 bottom 约束
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:shadowView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.superview addConstraint:bottomConstraint];
    shadowView.layer.shadowColor = color.CGColor;
    shadowView.layer.shadowOffset = offsert;
    shadowView.layer.shadowOpacity = 0.5;
    shadowView.layer.shadowRadius = 2;
    shadowView.clipsToBounds = NO;
    
}

- (UIViewController *)viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
